<?php if(!empty($dataResult)):?>
	<div id="playlist_player">
		<div id="playlist_frame">
			<div id="playlist_object">
				<!--Vdo Player-->
				<div id="vdoPlayer"></div>
			</div>
			<div id="playlist_ads">
				<iframe id='a1d6824b' name='a1d6824b' src='http://ads.you2play.com/openx/delivery/afr.php?zoneid=173&amp;cb=INSERT_RANDOM_NUMBER_HERE' frameborder='0' scrolling='no' width='330' height='382'><a href='http://ads.you2play.com/openx/delivery/ck.php?n=ab754587&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://ads.you2play.com/openx/delivery/avw.php?zoneid=173&amp;cb=INSERT_RANDOM_NUMBER_HERE&amp;n=ab754587' border='0' alt='' /></a></iframe>				
			</div>	
		</div>
		<div id="playlist_cover">
			<?php if($this->router->method == "you2play"):?>
				<div class="image">
					<?php echo exec_image(array(
						'folder' => 'playlist',
						'dimension' => '265x120',
						'file_name' => @$dataPlaylistSelect->playlist_file_name.'.'.@$dataPlaylistSelect->playlist_file_ext,
						'alt' => ''.@$dataPlaylistSelect->playlist_name.'',	
						'title' => ''.@$dataPlaylistSelect->playlist_name.''
						)
					);?>			
				</div>
			<?php else:?>
				<div class="image">
					<?php if(!empty($dataPlaylistSelect->member_id)):?>
						<?php echo image_asset('playlist/'.@$dataPlaylistSelect->playlist_file_name.'.'.@$dataPlaylistSelect->playlist_file_ext,'',array('width' => 265,'height' => 120));?>
					<?php else:?>
						<?php echo image_asset('playlist/mylist.jpg','',array('width' => 265,'height' => 120));?>
					<?php endif;?>
				</div>
			<?php endif;?>
			<?php if(!empty($dataPlaylistSelect)):?>
				<div class="description">
					<?php if($this->uri->segment(2) == $member['fb_id']):?>
						<a href="#update_playlist_info" id="update_playlist_info"></a>
					<?php endif;?>
					<h2><?php echo $dataPlaylistSelect->playlist_name;?></h2>
					
					<?php if(!empty($dataPlaylistSelect->member_id)):?>
						<span>โดยสมาชิก <?php echo $dataPlaylistSelect->username;?></span>
					<?php else:?>
						<span><?php echo $dataPlaylistSelect->playlist_brief;?></span>
					<?php endif;?>
				</div>
				<div class="counting">
					<div class="view">
						<span><?php echo number_format($dataPlaylistSelect->views);?> ครั้ง</span>
					</div>
					<?php if($this->uri->segment(2) != ""):?>
						<?php if($this->router->method == "member" && $is_activity_time):?>			
							<div class="votePlaylistActivity">
								<span class="score"><?php echo $dataPlaylistSelect->vote_activity;?></span>
							</div>
						<?php endif;?>
						<div class="votePlaylist">
							<a href="javascript:void(0);" onclick="javascript:votePlaylist(this);"></a>
							<span class="score"><?php echo $dataPlaylistSelect->vote;?></span>
						</div>	
						<?php if($this->uri->segment(2) == $member['fb_id']):?>
							<div id="is_publish">คลิ๊กที่นี่เพื่อแชร์สถานีเพลงของคุณ&nbsp;<input type="checkbox" value="1" <?php echo ($dataPlaylistSelect->is_publish == "1" ? 'checked="checked"':'');?>/></div>
						<?php endif;?>						
					<?php endif;?>											
				</div>				
			<?php else:?>
				<div class="description">
					<h2>You2PlayList (Guest)</h2>
					- เพลย์ลิสต์นี้จะหายไป เมื่อไม่ได้เข้าเว็บ You2Play นานกว่า 3 วัน<br />
					- เพลย์ลิสต์นี้เป็นระบบปิดและไม่สามารถนำไปแชร์ให้เพื่อนๆฟังได้<br />
					- สมัครสมาชิกกับ You2Play เพื่อสร้างเพลย์ลิสต์ได้ไม่มีวันหมดอายุ <a href ="javascript:void(0)" onclick="javascript:fbLogin(this);">คลิ๊กที่นี่</a>
				</div>
			<?php endif;?>
		</div>
		<?php if($this->uri->segment(2) != ""):?>
			<!--Facebook Plugin-->
			<?php echo $module_facebook_plugins_share;?>	
		<?php else:?>
			<br />		
		<?php endif;?>
		<!--Playlist List-->
		<div id="playlist_list">
			<div id="playlist_control">
				<div class="image">
					<?php echo exec_image(array(
						'folder' => ($dataResult[0]->content_type == "video" ? 'video':'album'),
						'dimension' => '120x120',
						'file_name' => @$dataResult[0]->album_file_name.'.'.@$dataResult[0]->album_file_ext,
						'alt' => ''.@$dataResult[0]->song_file_name.'',	
						'title' => ''.@$dataResult[0]->song_file_name.''
						)
					);?>				
				</div>
				<div class="description">
					<div class="subject">เพลง :</div><div class="detail"><?php echo $dataResult[0]->song_name;?></div>
					<div class="subject">ศิลปิน :</div>
					<div class="detail">
						<?php foreach($dataResult[0]->artist as $key => $artist):?>
							<?php echo ($key == 0 ? '':'/');?>
							<span><?php echo $artist->artist_name;?></span>
						<?php endforeach;?>					
					</div>
					<a href="<?php echo $dataResult[0]->content_url;?>" class="contentUrl" target="_blank">&laquo; ลิงค์ไปยัง เพลง/MV นี้ &raquo;</a>
				</div>
				<div class="control">
					<a href="javascript:void(0);" class="playlistControl btnRw" onclick="javascript:controlPlaylist(this);" title="เพลงก่อนหน้า"></a>
					<a href="javascript:void(0);" class="playlistControl btnPlay" onclick="javascript:controlPlaylist(this);" title="เล่น/หยุด เพลง"></a>
					<a href="javascript:void(0);" class="playlistControl btnStop" onclick="javascript:controlPlaylist(this);" title="ปิดเพลง"></a>
					<a href="javascript:void(0);" class="playlistControl btnFw" onclick="javascript:controlPlaylist(this);" title="เพลงถัดไป"></a>				
				</div>
				<?php if($this->uri->segment(2) == "" || $this->uri->segment(2) == $member['fb_id']):?>
					<a href="javascript:void(0);" class="deleteAll" onclick="javascript:remove_from_playlist(this,-1);">ลบทั้งหมด</a>
				<?php endif;?>
			</div>
			<ul id="playlist_item">
				<?php foreach($dataResult as $key => $value):?>
					<li id="song_<?php echo $key.'-'.$value->content_id;?>">
						<?php if($this->uri->segment(2) == "" || $this->uri->segment(2) == $member['fb_id']):?>
							<a href="javascript:void(0);" class="action remove" title="ลบทิ้ง" onclick="javascript:remove_from_playlist(this,<?php echo $key;?>)"></a>
							<a href="javascript:void(0);" class="action move" title="เปลี่ยนตำแหน่ง"></a>	
						<?php endif;?>			
						<a href="javascript:void(0);" class="activeItem <?php echo ($key==0 ? 'active':'');?>" alt="เล่นเพลงนี้" title="เล่นเพลงนี้" onclick="javascript:urlHash(<?php echo $key;?>);">
							<span class="image">
								<?php echo exec_image(array(
									'folder' => ($value->content_type == "video" ? 'video':'album'),
									'dimension' => '120x120',
									'file_name' => @$value->album_file_name.'.'.@$value->album_file_ext,
									'alt' => ''.@$value->song_file_name.'',	
									'title' => ''.@$value->song_file_name.''
									)
								);?>							
							</span>
							<span class="song_name"><?php echo $value->song_name;?></span>
							<span class="artist_name">
							<?php foreach($value->artist as $key2 => $artist):?>
								<?php echo ($key2 == 0 ? '':'/');?>
								<span><?php echo $artist->artist_name;?></span>
							<?php endforeach;?>
							</span>
							<span class="content_url"><?php echo $value->content_url;?></span>
						</a>
					</li>
				<?php endforeach;?>
			</ul>
		</div>
	</div>
	<?php 
		if($this->uri->segment(2) == "Jazz_Cafe" || $this->uri->segment(2) == "indiesong_july_2014" || $this->uri->segment(2) == "lovehurt"):
			ini_set('display_errors', 1);
		endif;
	?>
	<?php if(!empty($dataResult)):?>
	<script type="text/javascript" language="javascript">
	(function($) {
	/*whatever jQuery stuff*/
		$(function(){
			var image_path = '<?php echo other_asset_url("uploads/album/images/original")."/";?>';
			var dataPlaylist = JSON.parse('<?php echo addslashes(json_encode($dataResult));?>');
			var jwplaylist = '[';
			var date_path;
			for(i=0;i < dataPlaylist.length;i++){
				//var content_path = "<?php echo other_asset_url('uploads').'/';?>";
				var content_path = "http://vstatic.you2play.com/2013/assets/uploads/";
				content_path += dataPlaylist[i].content_type+"/files/";
				date_path = dataPlaylist[i].create_date.substring(0,4)+'/'+dataPlaylist[i].create_date.substring(5,7)+'/'+dataPlaylist[i].create_date.substring(8,10)+'/';
				/*Check Relate Song if not then change image path to video*/
				if(dataPlaylist[i].is_relate_song == "0"){
					image_path = '<?php echo other_asset_url("uploads/video/images/original")."/";?>';
				} else {
					image_path = '<?php echo other_asset_url("uploads/album/images/original")."/";?>';
				}
				if(dataPlaylist[i].is_youtube == "1"){
					jwplaylist += '{"image":"'+image_path+dataPlaylist[i].album_file_name+'.'+dataPlaylist[i].album_file_ext+'","file":"'+dataPlaylist[i].youtube_link+'","title":"'+dataPlaylist[i].song_name.replace(/"/g, '\\"').trim()+'"},';
				} else {
					jwplaylist += '{"image":"'+image_path+dataPlaylist[i].album_file_name+'.'+dataPlaylist[i].album_file_ext+'","file":"'+content_path+date_path+dataPlaylist[i].song_file_name+'.'+dataPlaylist[i].song_file_ext+'","title":"'+dataPlaylist[i].song_name.replace(/"/g, '\\"').trim()+'"},';
				}
			}
			jwplaylist = jwplaylist.substr(0,jwplaylist.length-1)+']';
			dataPlaylist = JSON.parse(jwplaylist);
			jwplayer("vdoPlayer").setup({
				repeat: true,
				width: 640,
				height: 382,
				primary: "flash",
				playlist:dataPlaylist,
               	"provider": "http",
        		"startparam": "starttime"					
			});	
			
			jwplayer().onPlay(function() {
				var indexAt = jwplayer().getPlaylistIndex();
				var master = $('#playlist_item').find('a.activeItem');
				var image = master.eq(indexAt).find('.image').html();
				var song_name = master.eq(indexAt).find('.song_name').text();
				var artist_name = master.eq(indexAt).find('.artist_name').text();
				var content_url = master.eq(indexAt).find('.content_url').text();
				master.removeClass('active');
				master.eq(indexAt).addClass('active');	
				/*Set Active item background*/
				$('#playlist_item').find('a.activeItem').removeClass('active');
				$('#playlist_item').find('a.activeItem:eq('+indexAt+')').addClass('active');
				/*Set Song/Artist name*/
				$('#playlist_control').find('.image').html(image);
				$('#playlist_control').find('.detail').eq(0).text(song_name);
				$('#playlist_control').find('.detail').eq(1).text(artist_name);
				$('#playlist_control').find('.contentUrl').attr('href',''+content_url+'');
				$('.playlistControl.btnPlay,.playlistControl.btnPause').attr('class','playlistControl btnPause');
			});

			jwplayer().onPause(function() {
				$('.playlistControl.btnPlay,.playlistControl.btnPause').attr('class','playlistControl btnPlay');
			});	
			
			jwplayer().onComplete(function(){
				var playlist = jwplayer().getPlaylist();
				var next_index = jwplayer().getPlaylistIndex()+1;
				if(next_index < playlist.length){
					urlHash(next_index);
				} else {
					urlHash(0);
				}
			});	
			
			if(window.location.hash != ''){
				var hash_tag = parseInt(window.location.hash.replace('#',''),10);
				$('#playlist_item').find('a.activeItem').removeClass('active');
				$('#playlist_item').find('a.activeItem:eq('+hash_tag+')').addClass('active');
				jwplayer().playlistItem(hash_tag);
			}
		});	
	})(jQuery);

	function urlHash(indexAt)
	{
		var hash_tag = window.location.hash;
		window.location.href = window.location.href.replace(hash_tag,'')+'#'+indexAt;
		window.location.reload();
	}

	function controlPlaylist(e)
	{
		var mode = $(e).attr('class');
		switch(mode){
			case 'playlistControl btnRw':
				var indexAt = jwplayer().getPlaylistIndex()-1;
				$('.playlistControl.btnPlay,.playlistControl.btnPause').attr('class','playlistControl btnPause');
				jwplayer().playlistItem(indexAt);
			break;
			case 'playlistControl btnPlay':
			case 'playlistControl btnPause':
				if(mode == "playlistControl btnPlay"){
					$(e).attr('class','playlistControl btnPause');
				} else {
					$(e).attr('class','playlistControl btnPlay');
				}
				jwplayer().play();
			break;
			case 'playlistControl btnStop':
				$('.playlistControl.btnPlay,.playlistControl.btnPause').attr('class','playlistControl btnPlay');	
				jwplayer().stop();
			break;
			case 'playlistControl btnFw':	
				var indexAt = jwplayer().getPlaylistIndex()+1;
				$('.playlistControl.btnPlay,.playlistControl.btnPause').attr('class','playlistControl btnPause');			
				jwplayer().playlistItem(indexAt);
			break;					
		}
	}
	</script>
	<?php endif;?>
<?php else: //no playlist in system?>
	<div id="playlist">
		<div class="empty">คุณยังไม่มีเพลงในระบบ คุณสามารถเพิ่มเพลงได้ง่ายๆ จากคำแนะนำด้านล่าง</div>
		<center>	
		</center>
		<?php echo image_asset('playlist/howto.jpg','',array('width' => 970,'height' => 1150));?>
	</div>
<?php endif;?>
<script type="text/javascript" language="javascript">
function fbLogin(e)
{
	FB.login(function(response)
		{
			$('#fbStatus').find('.spinner').show();
			if (response.authResponse)
			{
				// connected
				FB.api('/me', function(response) {
					$.ajax({
						url:web_path+'ajax_member_login',
						type:'post',
						data:({
							fb_id:response.id,
							username:response.username
						}),
						dataType:'json'
					}).done(function(data){
						$('#fbStatus').find('.spinner').hide();
						if(data.error == 1){
							alert(data.msg);
						} else {
							window.location.href = web_path+'member/'+data.member.fb_id;
						}
					});									
				});
			} else
			{
				// cancelled
				//window.location.href = window.location.href.replace(window.location.hash,'');
			}
		});
}

function memberLogout()
{
	$.ajax({
		url:web_path+'ajax_member_logout',
		type:'get'
	}).done(function(data){
		//Show Result
		window.location.href = web_path+'member';
	});					
}
</script>
<?php if(!empty($dataPlaylistSelect)):?>
<script type="text/javascript" language="javascript">
	function votePlaylist(e)
	{
		var fb_id = "<?php echo $member['fb_id'];?>";
		if(fb_id == ""){
			alert('กรุณาล็อคอินก่อนทำการโหวตค่ะ');
		} else {
			var playlist_id = "<?php echo $dataPlaylistSelect->playlist_id;?>";
			$.ajax({
				type:'post',
				url:web_path+'ajax_vote_playlist',
				data:({
					playlist_id:playlist_id,
					fb_id:fb_id
				})
			}).done(function(data){
				if(data != ""){
					alert(data);
				} else {
					$(e).closest('.counting').find('.score').each(function(){
						var score = new Number($(this).text());
						$(this).text(score+1);
						$(this).css({'font-size':'20px'});
						$(this).animate({
							fontSize : '13px'
						});
					});
				}	
			});		
		}
	}
</script>
<?php endif;?>
<?php if($this->uri->segment(2) != "" && $this->router->method == "member"):?>
	<script type="text/javascript" language="javascript">
	(function($) {
	/*whatever jQuery stuff*/
		$(function(){	
			$('#is_publish input:checkbox').change(function(){
				var status = ($(this).prop("checked") ? 1:0);
				$.ajax({
					type:'post',
					url:web_path+'ajax_update_status',
					data:({
						is_publish:status
					}),
					async:true
				});
			});
			
			$('#update_playlist_info').click(function(){
				$(this).colorbox({
					inline:true,
					width:"405px",
					href:"#update_playlist"
				});
			});
		});	
	})(jQuery);	
	
	function update_playlist()
	{
		var playlist_name = $('#playlist_name').val();
		var playlist_img = $('input[name="playlist_img"]:checked').val();
		$('#update_playlist').find('.spinner').show();
		$.ajax({
			type:'post',
			url:web_path+'ajax_update_playlist',
			data:({
				playlist_name:playlist_name,
				playlist_img:playlist_img
			}),
			async:true		
		}).done(function(data){
			window.location.href = window.location.href.replace(window.location.hash,'');
		});
	}			
	</script>
	<!-- This contains the hidden content for inline calls -->
	<div style="display:none">
		<div id="update_playlist" style="padding:10px; background:#fff;">
			<ul>
				<li class="left">ชื่อเพลลิสต์:</li>
				<li class="right"><input type="text" id="playlist_name" name="playlist_name" size="30" maxlength="255" value="<?php echo $dataPlaylistSelect->playlist_name;?>"/></li>
				<li class="left">เลือกปก:</li>
				<li class="right">
					<ol>
						<?php for($i=1;$i<=7;$i++):?>
						<li>
							<div class="btnRadio"><input type="radio" name="playlist_img" value="mylist_<?php echo sprintf('%02d',$i);?>.jpg" <?php echo ($dataPlaylistSelect->playlist_file_name ==  'mylist_'.sprintf('%02d',$i) ? 'checked="checked"':'');?>/></div>
							<div class="playlistImg"><?php echo image_asset('playlist/mylist_'.sprintf('%02d',$i).'.jpg','',array('width' => 120));?></div>
						</li>
						<?php endfor;?>
					</ol>
				</li>
			</ul>
			<ul>	
				<li class="left">&nbsp;</li>
				<li class="right"><input type="button" value="แก้ไขข้อมูล" class="submit" onclick="javascript:update_playlist();"/>&nbsp;&nbsp;&nbsp;<span class="spinner">&nbsp;</span></li>	
			</ul>
		</div>
	</div>		
<?php endif;?>
<?php if($this->router->method == "member"):?>
	<script type="text/javascript" language="javascript">
	(function($) {
	/*whatever jQuery stuff*/
		$(function(){
			//Drag && Drop
			$( "#playlist_item" ).sortable(
			{
				connectWith: "li",
				cursor: 'move',
				handle: '.move',
				placeholder: 'placeholder',
				forcePlaceholderSize: true,
				opacity: 0.4,
				start: function(event, ui)
				{
					var start_pos = ui.item.index();
	            	ui.item.data('start_pos', start_pos);	
				},
				update: function(event, ui)
				{
					//do process
					var data = $(this).sortable('serialize');
					$.ajax({
						type:'post',
						url:web_path+'ajax_swap_to_playlist',
						data:data
					}).done(function(data){
						/*window.location.href = window.location.href;*/
						var start_pos = ui.item.data('start_pos');
						var current_pos = ui.item.index();	
						var playlistItem = new Array();
						var playlist = jwplayer().getPlaylist();
						for (var i=0;i<playlist.length;i++)
						{
							if(start_pos > current_pos){
								if(current_pos <= i && i <= start_pos){
									if(i == current_pos){
										playlistItem[i] = jwplayer().getPlaylistItem(start_pos);
									} else {
										playlistItem[i] = jwplayer().getPlaylistItem(i-1);
									}
								} else {
									playlistItem[i] = jwplayer().getPlaylistItem(i);
								}
							} else {
								if(start_pos <= i && i <= current_pos){
									if(i == current_pos){
										playlistItem[i] = jwplayer().getPlaylistItem(start_pos);
									} else {
										playlistItem[i] = jwplayer().getPlaylistItem(i+1);
									}
								} else {
									playlistItem[i] = jwplayer().getPlaylistItem(i);
								}						
							}
						}
						jwplayer().load(playlistItem);	
						/*Play Auto*/
						jwplayer().play();
					});		
				}
			}).disableSelection();			
		});	
	})(jQuery);	
	
	//Remove Playlist
	function remove_from_playlist(e,id){
		$.ajax({
			type:'post',
			url:web_path+'ajax_remove_from_playlist',
			data:({
				id:id
			})
		}).done(function(data){
			if(id == -1){
				window.location.href = window.location.href.replace(window.location.hash,'');
			} else {
				//Remove items
				$(e).closest('li').fadeOut(function(){
					window.location.href = window.location.href.replace(window.location.hash,'');
				});				
			}
		});
	}
	</script>		
<?php endif;?>